package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ContractPayDetailMapper contractPayDetailMapper;
    @Autowired
    private IBussinessDeptService bussinessDeptService;
    @Autowired
    private IDeliveryDeptService deliveryDeptService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private IAreaService areaService;
    @Autowired
    private ISysPostService sysPostService;


    /**
     * 查询合同
     *
     * @param id 合同主键
     * @return 合同
     */
    @Override
    public Contract selectContractById(Long id) {
        return contractMapper.selectContractById(id);
    }

    /**
     * 查询合同列表
     *
     * @param contract 合同
     * @return 合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract) {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同
     *
     * @param contract 合同
     * @return 结果
     */
    @Transactional
    @Override
    public int insertContract(Contract contract) {
        int rows = contractMapper.insertContract(contract);
        insertContractPayDetail(contract);
        return rows;
    }

    /**
     * 修改合同
     *
     * @param contract 合同
     * @return 结果
     */
    @Transactional
    @Override
    public int updateContract(Contract contract) {
        contractMapper.deleteContractPayDetailByContractId(contract.getId());
        insertContractPayDetail(contract);
        return contractMapper.updateContract(contract);
    }

    /**
     * 批量删除合同
     *
     * @param ids 需要删除的合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractByIds(Long[] ids) {
        contractMapper.deleteContractPayDetailByContractIds(ids);
        return contractMapper.deleteContractByIds(ids);
    }

    /**
     * 删除合同信息
     *
     * @param id 合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractById(Long id) {
        contractMapper.deleteContractPayDetailByContractId(id);
        return contractMapper.deleteContractById(id);
    }

    @Override
    public int add(Contract contract) {

        Long businessId = contract.getBusinessId();
        Long deliverId = contract.getDeliverId();

        BussinessDept bussinessDept = bussinessDeptService.getById(businessId);
        DeliveryDept deliveryDept = deliveryDeptService.getById(deliverId);

        contract.setBusinessName(bussinessDept.getName());
        contract.setDeliverName(deliveryDept.getName());

        boolean save = save(contract);
        if (save) {
            Long id = contract.getId();
            List<ContractPayDetail> contractPayDetailList = contract.getContractPayDetailList();
            for (ContractPayDetail detail : contractPayDetailList) {
                Area area = areaService.getById(detail.getAreaId());
                SysPost sysPost = sysPostService.selectPostById(detail.getPostId());
                detail.setAreaName(area.getName());
                detail.setPostName(sysPost.getPostName());
                detail.setContractId(id);
                contractPayDetailMapper.insert(detail);
            }
            return 1;
        }
        return 0;
    }

    @Override
    public int updateContract2(Contract contract) {

        boolean b = updateById(contract);
        if (b) {
            Long id = contract.getId();
            LambdaQueryWrapper<ContractPayDetail> qw = Wrappers.lambdaQuery();
            qw.eq(ContractPayDetail::getContractId, id);
            contractPayDetailMapper.delete(qw);

            List<ContractPayDetail> contractPayDetailList = contract.getContractPayDetailList();
            for (ContractPayDetail detail : contractPayDetailList) {
                detail.setContractId(id);
                contractPayDetailMapper.insert(detail);
            }
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteByIds(Long[] ids) {

        List<Long> list = Arrays.asList(ids);
        LambdaQueryWrapper<Customer> qw = Wrappers.lambdaQuery();
        qw.in(Customer::getContractId, list);
        Long aLong = customerMapper.selectCount(qw);
        if (aLong > 0) {
            return 0;
        }

        LambdaQueryWrapper<ContractPayDetail> qw2 = Wrappers.lambdaQuery();
        boolean b = removeByIds(list);
        if (b) {
            qw2.in(ContractPayDetail::getContractId, ids);
            contractPayDetailMapper.delete(qw2);
            return ids.length;
        }
        return 0;
    }


    /**
     * 新增付费模式明细信息
     *
     * @param contract 合同对象
     */
    public void insertContractPayDetail(Contract contract) {
        List<ContractPayDetail> contractPayDetailList = contract.getContractPayDetailList();
        Long id = contract.getId();
        if (StringUtils.isNotNull(contractPayDetailList)) {
            List<ContractPayDetail> list = new ArrayList<ContractPayDetail>();
            for (ContractPayDetail contractPayDetail : contractPayDetailList) {
                contractPayDetail.setContractId(id);
                list.add(contractPayDetail);
            }
            if (list.size() > 0) {
                contractMapper.batchContractPayDetail(list);
            }
        }
    }
}
